//
//  DSSHQiangRelatedTaskVC.m
//  DSSH
//
//  Created by lcy on 16/4/13.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "DSSHQiangRelatedTaskVC.h"
#import "UIView+Round.h"
#import "QiangTaskDetailCell.h"
#import "DSSHTaskPointDistributeVC.h"
@interface DSSHQiangRelatedTaskVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray * _isOpen;

}
@property(nonatomic,strong)UITableView*relatedTableView;
@property(nonatomic,strong)NSMutableArray*imageViewArray;
@end

@implementation DSSHQiangRelatedTaskVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewAndData];
    // Do any additional setup after loading the view.
}
-(void)setupViewAndData
{
    self.title = @"关联任务";
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIBarButtonItem * backBarBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarBtnClick)];
    self.navigationItem.leftBarButtonItem = backBarBtn;
    
    //添加详情表
    self.relatedTableView =[[ UITableView alloc]initWithFrame:CGRectMake(5, 69, ZYAppWidth-10, ZYAppHeight-69) style:UITableViewStyleGrouped];
    self.relatedTableView.delegate = self;
    self.relatedTableView.dataSource = self;
    [self.relatedTableView getRoundView:5];
    self.relatedTableView.layer.borderWidth = 1;
    self.relatedTableView.layer.borderColor = [[UIColor grayColor]CGColor];
    
    [self.view addSubview:self.relatedTableView];
    self.imageViewArray = [[NSMutableArray alloc]init];
    _isOpen = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO],[NSNumber numberWithBool:NO], nil];
}
-(void)setupConstraints
{
    
}
-(void)backBarBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_isOpen[section] boolValue]== YES)
    {
        return 2;
        
    }else
    {
        return 0;
    }
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        QiangTaskDetailCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[NSBundle mainBundle]loadNibNamed:@"QiangTaskDetailCell" owner:nil options:nil][0];
        }
        return cell;
    }else
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
        if (!cell)
        {
            cell = [[UITableViewCell alloc]init];
        }
        UIButton * pointBtn = [UIButton new];
        [pointBtn setTitle:@"点位分布" forState:UIControlStateNormal];
        pointBtn.titleLabel.font = FONT(13);
        [pointBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [pointBtn addTarget:self action:@selector(clickPointBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:pointBtn];
        
        pointBtn.layer.borderWidth = 1;
        pointBtn.layer.borderColor = [[UIColor grayColor]CGColor];
        [pointBtn getRoundView:5];
        [pointBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(90*WIDTH_SCALE, 30*HIGHT_SCALE));
            make.centerX.mas_equalTo(cell.contentView.mas_centerX);
            make.centerY.mas_equalTo(cell.contentView.mas_centerY);
        }];

        return cell;
    }
    
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton * headBtn = [UIButton new];
    headBtn.frame = CGRectMake(0, 0, ZYAppWidth-10, 44*HIGHT_SCALE);
    [headBtn setBackgroundColor:COLOR_NAVIGATE_BAR];
    [headBtn getRoundView:5];
    headBtn.tag = section;
    [headBtn addTarget:self action:@selector(clickHeadBtn:) forControlEvents:UIControlEventTouchUpInside];
    [headBtn setTitle:@"项目名称：未来之光" forState:UIControlStateNormal];
    headBtn.titleLabel.font = FONT(14);
    UIImageView * xiaLaImageView = [[UIImageView alloc]init];
    [headBtn addSubview:xiaLaImageView];
    if ([_isOpen[section] boolValue]== YES)
    {
        xiaLaImageView.image = [UIImage imageNamed:@"taskxiala"];
        [xiaLaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(headBtn.mas_right).with.offset(-10);
            make.centerY.mas_equalTo(headBtn.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(12*WIDTH_SCALE, 8*HIGHT_SCALE));
        }];
        
    }else
    {
        xiaLaImageView.image = [UIImage imageNamed:@"taskkuozhan"];
        [xiaLaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(headBtn.mas_right).with.offset(-10);
            make.centerY.mas_equalTo(headBtn.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(8*WIDTH_SCALE, 12*HIGHT_SCALE));
        }];
    }
    [self.imageViewArray addObject:xiaLaImageView];
    
    return headBtn;
}
-(void)clickHeadBtn:(UIButton*)button
{
    for (int i = 0; i<_isOpen.count; i++)
    {
        if ([_isOpen[i] boolValue] == YES)
        {
            [_isOpen replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:NO]];
        }
    }
    BOOL value = [_isOpen[button.tag] boolValue];
    value = !value;
    [_isOpen replaceObjectAtIndex:button.tag withObject:[NSNumber numberWithBool:value]];
    [self.relatedTableView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44*HIGHT_SCALE;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 270;
    }else
    {
        return 44*HIGHT_SCALE;
    }
}
-(void)clickPointBtn:(UIButton*)button
{
    DSSHTaskPointDistributeVC * vc = [[DSSHTaskPointDistributeVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
